package org.seven.jrdp.biz.t.service;

import java.util.Map;

import org.seven.jrdp.biz.t.mapper.TKqMxMapper;
import org.seven.jrdp.commons.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TKqMxService {
	@Autowired
	private TKqMxMapper tKqMxMapper;

	@Transactional(readOnly = true)
	public String getKqmc(String ggxh) {
		String kqmc = tKqMxMapper.getKqmc1(ggxh);
		if (kqmc == null) {
			kqmc = tKqMxMapper.getKqmc2(ggxh);
			if (kqmc == null) {
				kqmc = tKqMxMapper.getKqmc3(ggxh);
				if (kqmc == null) {
					throw new BizException("没有可用库区，不允许插入明细");
				}
			}
		}
		return kqmc;
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getByEwm(String ewm) {
		return tKqMxMapper.getByEwm(ewm);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getCpck(String ewm) {
		Map<String, Object> kcxx = tKqMxMapper.getByEwm(ewm);
		if (kcxx == null) {
			throw new BizException("二维码无效");
		} else {
			Map<String, Object> ckcp = tKqMxMapper.getCkcp((String) kcxx.get("ggxh"));
			if (ckcp == null) {
				throw new BizException("没有可出库的产品");
			} else {
				if (kcxx.get("ckId") == null) {
					throw new BizException("该产品未创建出库单");
				}
				if (!kcxx.get("ckId").equals(ckcp.get("ckId"))) {
					throw new BizException("该产品不允许出库");
				}
			}
		}
		return kcxx;
	}
}